package sqlAndIo.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;






import org.json.JSONException;
import org.json.JSONObject;

import com.fasterxml.jackson.databind.ObjectMapper;

import sqlAndIo.pojo.Book;
import sqlAndIo.util.DBUtil;


public class JSONBook {

	    public void saveToFile(){  
	    		
	        try {  
	            //写对象流的对象  
	            FileWriter fw=new FileWriter("book.json");  
	            BufferedWriter bf=new BufferedWriter(fw);
	            for(int i=1;i<10001;i++)
	            {
	            	Book book=new Book(i,"java"+i,new Date(),"bruce",50,true,true);
	            	JSONObject json = new JSONObject();//将java对象转换为json对象
	            	json.put("id", book.getId());
	            	json.put("name", book.getName());
	            	json.put("pubDate", DBUtil.dateToStr(book.getPubDate(), "yyyy-MM-dd HH:mm:ss"));
	            	json.put("author", book.getAuthor());
	            	json.put("price", book.getPrice());
	            	json.put("isNew", book.isNew());
	            	json.put("published", book.isPublished());
//	            	String  str = json.toString();//将json对象转换为字符串
//	            	bf.write(str+"\n");
	            	bf.write(json.toString()+"\n");
	            }
	           
	            bf.flush();
	            bf.close();                        //关闭文件流  
	        } catch (FileNotFoundException e) {  
	            e.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}   
	    } 
	   
	}  
